package com.example.smartcity_poverty.ui.AllService.OA.activity

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.MenuItem
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.smartcity_poverty.Base.BaseRecyclerAdapter
import com.example.smartcity_poverty.R
import com.example.smartcity_poverty.logic.Repository.api
import com.example.smartcity_poverty.logic.Repository.coroutine
import com.example.smartcity_poverty.logic.ServiceBean.OA.OaShowMeDataModel
import com.example.smartcity_poverty.logic.util.Util.show
import kotlinx.android.synthetic.main.activity_oa_show_me_data.*
import kotlinx.android.synthetic.main.re_oa_show_me_data.*
import kotlinx.coroutines.Dispatchers

/**
 * 门诊科室
 */
class OaShowMeDataActivity : AppCompatActivity() {

    val name by lazy { intent.getStringExtra("name") }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        supportActionBar?.setDisplayHomeAsUpEnabled(true)
        setContentView(R.layout.activity_oa_show_me_data)
        title = "门诊科室"

        runThread()
    }

    private fun runThread() = coroutine(Dispatchers.IO) {
        // 获取数据列表
        getMeDataList()
    }

    /**
     * 获取数据列表
     */
    val array = ArrayList<OaShowMeDataModel.RowsBean>()
    private suspend fun getMeDataList() {
        val result = api.getOaShowMeData()
        if (result.code == 200) {
            runOnUiThread {
                // 添加数据到集合
                array.addAll(result.rows!!)
                // RecyclerView适配器
                oaShowMeDataHomeRv.apply {
                    this.layoutManager = LinearLayoutManager(context)
                    this.adapter = OaShowMeDataAdapter(array, R.layout.re_oa_show_me_data)
                    this.adapter?.notifyItemChanged(array.size)
                }
            }
        } else runOnUiThread { result.msg?.show() }
    }


    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        finish()
        return super.onOptionsItemSelected(item)
    }


    /**
     * 门诊科室主页列表适配器
     */
    inner class OaShowMeDataAdapter(
        val list: MutableList<OaShowMeDataModel.RowsBean>,
        layoutId: Int
    ) : BaseRecyclerAdapter<OaShowMeDataModel.RowsBean>(list, layoutId) {
        override fun setData(
            view: BaseViewHolder.ViewFind,
            data: OaShowMeDataModel.RowsBean,
            position: Int,
            holder: BaseViewHolder
        ) {
            // 填充数据
            val d = list[position]
            val fv = holder.viewFind
            val con = holder.viewFind.view?.context

            fv.oaShowMeDataName.text = d.categoryName

            // 点击进入
            holder.itemView.setOnClickListener {
                con?.startActivity(Intent(con, OaShowMeAoActivity::class.java).apply {
                    putExtra("id", d.id.toString())
                    putExtra("name", name.toString())
                })
            }
        }
    }
}